package com.bustle.web.mbean.request;

import java.util.Date;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import com.bustle.facade.proxy.AuthenticationDtoManagerProxy;
import com.bustle.user.data.User;
import com.bustle.user.dto.address.AddressDto;
import com.bustle.user.dto.user.UserDto;
import com.bustle.web.Report;
import com.bustle.web.mbean.session.UserMBean;

@ManagedBean
@RequestScoped
public class InscriptionMBean
{
  public String suscribe()
  {
    try
    {
      UserDto user = new UserDto();
      user.setFirstName(firstName);
      user.setLastName(lastName);
      user.setBirthday(birthday);
      user.setEmail(email);
      user.setType(User.Type.CUSTOMER);
      AddressDto addressDto = new AddressDto();
      addressDto.setCity(addressBean.getCity());
      addressDto.setName(addressBean.getName());
      addressDto.setPostcode(addressBean.getPostCode());
      user.setAddress(addressDto);
      authenticationDtoManager.createUser(user, authenticationBean.getLogin(), authenticationBean.getPassword());

      userMBean.setId(user.getId());
      return "monCompte.jsf";
    }
    catch (Exception anException)
    {
      Report.error("Unable to suscribe " + firstName + " " + lastName, anException);
    }
    return null;
  }
  public String getFirstName()
  {
    return firstName;
  }

  public void setFirstName(String aFirstName)
  {
    firstName = aFirstName;
  }

  public String getLastName()
  {
    return lastName;
  }

  public void setLastName(String aLastName)
  {
    lastName = aLastName;
  }

  public Date getBirthday()
  {
    return birthday;
  }

  public void setBirthday(Date aBirthday)
  {
    birthday = aBirthday;
  }

  public String getEmail()
  {
    return email;
  }

  public void setEmail(String anEmail)
  {
    email = anEmail;
  }

  public AddressBean getAddressBean()
  {
    return addressBean;
  }

  public void setAddressBean(AddressBean anAddressBean)
  {
    addressBean = anAddressBean;
  }

  public AuthenticationBean getAuthenticationBean()
  {
    return authenticationBean;
  }

  public void setAuthenticationBean(AuthenticationBean anAuthenticationBean)
  {
    authenticationBean = anAuthenticationBean;
  }

  public void setUserMBean(UserMBean aUserMBean)
  {
    userMBean = aUserMBean;
  }

  private String firstName;
  private String lastName;
  private Date birthday;
  private String email;
  private AddressBean addressBean = new AddressBean();
  private AuthenticationBean authenticationBean = new AuthenticationBean();

  @EJB
  protected AuthenticationDtoManagerProxy authenticationDtoManager;
  @ManagedProperty("#{userMBean}")
  private UserMBean userMBean;

  static public final String SCREEN = "authentication.jsf";


  static public class AuthenticationBean
  {
    public String getLogin()
    {
      return login;
    }

    public void setLogin(String aLogin)
    {
      login = aLogin;
    }

    public String getPassword()
    {
      return password;
    }

    public void setPassword(String aPassword)
    {
      password = aPassword;
    }

    private String login;
    private String password;
  }

  static public class AddressBean
  {
    public String getName()
    {
      return name;
    }

    public void setName(String aName)
    {
      name = aName;
    }

    public String getPostCode()
    {
      return postCode;
    }

    public void setPostCode(String aPostCode)
    {
      postCode = aPostCode;
    }

    public String getCity()
    {
      return city;
    }

    public void setCity(String aCity)
    {
      city = aCity;
    }

    protected String name;
    protected String postCode;
    protected String city;
  }
}
